<template>
  <div class="template-section">
    <div
      class="goods-item"
      v-for="(item, index) in goodsList"
      :key="index"
      :style="goodsItemSpace"
    >
      <div class="goods-banner">
        <img v-if="item.cover_pic" :src="item.cover_pic" alt="" />
        <img
          v-else
          class="default-img"
          src="@/assets/images/diy/default_img.png"
          alt=""
        />
      </div>
      <div class="goods-info">
        <p
          class="goods-name"
          :style="{ color: propsData.params.producNameColor }"
        >
          {{ item.name }}
        </p>
        <div class="price-box">
          <span class="price" :style="{ color: propsData.params.priceColor }">
            <span>￥</span>
            <span>{{ item.price | priceBeforeZero }}</span>
            <span>{{ item.price | priceAfterZero }}</span>
          </span>
          <span
            class="origin-price"
            :style="{ color: propsData.params.originPriceColor }"
            v-if="propsData.params.showPrice"
            >￥{{ item.original_price }}</span
          >
        </div>
        <p
          class="sales"
          v-if="propsData.params.showSales"
          :style="{ color: propsData.params.saleColor }"
        >
          已售 {{ item.sales || 0 }}
        </p>
        <div
          class="buy-btn"
          :style="
            propsData.params.buyStyle == 'add' ? 'background: #fa2c1b;' : ''
          "
          v-if="propsData.params.showBuyBtn"
        >
          <i
            v-if="propsData.params.buyStyle == 'add'"
            class="iconfont icon-jiahao"
          ></i>
          <!-- <i
            v-if="propsData.params.buyStyle == 'cart'"
            class="iconfont icon-weibiaoti-14"
          ></i> -->
          <img
            style="width: 100%"
            v-if="
              propsData.params.buyStyle == 'cart' && propsData.params.buyBtnImg
            "
            :src="propsData.params.buyBtnImg"
            alt=""
          />
        </div>
        <div
          v-if="propsData.params.buyStyle == 'word'"
          class="buy-text"
          :class="propsData.params.buyBtnText.length > 3 ? 'inline-block' : ''"
        >
          <span>{{ propsData.params.buyBtnText || "购买" }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "",
  props: {
    propsData: {
      type: Object,
      default: {},
    },
    goodsList: {
      type: Array,
      default: [],
    },
  },
  data() {
    return {};
  },
  mounted() {},
  filters: {
    priceBeforeZero(val) {
      if (val.indexOf(".") < 0) return `${val}.`;
      return val.substr(0, val.indexOf("."));
    },
    priceAfterZero(val) {
      if (val.indexOf(".") < 0) return "00";
      return val.substr(val.indexOf("."), val.length);
    },
  },
  watch: {},
  computed: {
    goodsItemSpace() {
      let out = this.propsData.params.goodsItemOutSpace || "0";
      let inner = this.propsData.params.goodsItemInnerSpace || "0";
      let css = {};
      if (out) {
        css["margin"] = `0 ${out}px 0 0`;
      }
      if (inner) {
        css["padding"] = `${inner}px`;
      }
      return css;
    },
  },
  methods: {},
};
</script>

<style lang="scss" scoped>
.template-section {
  width: 93%;
  display: flex;
  justify-content: flex-start;
  margin: 0 auto;
  overflow: hidden;
  padding-bottom: 12px;
  .goods-item {
    width: 28.49%;
    // height: 220px;
    background: #ffffff;
    border-radius: 5px;
    display: flex;
    flex-direction: column;
    position: relative;
    padding-bottom: 6px;
    margin: 0 6px 6px 6px;
    flex-shrink: 0;
    .goods-banner {
      width: 100%;
      height: 100px;
      background: #f2f2f2;
      border-radius: 5px 5px 0px 0px;
      display: flex;
      justify-content: center;
      align-items: center;
      overflow: hidden;
      img {
        width: 100%;
        height: 100%;
      }
      .default-img {
        width: 56px;
        height: 43px;
      }
    }
    .goods-info {
      width: 100%;
      padding: 0 5px 5px;
      .goods-name {
        margin-top: 4px;
        font-size: 14px;
        font-family: PingFang SC;
        font-weight: 600;
        color: rgba(29, 29, 29, 0.94);
        word-break: break-all;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2; /* 这里是超出几行省略 */
        overflow: hidden;
        margin-bottom: 0;
        line-height: 20px;
      }
      .price-box {
        margin-top: 6px;
        .price {
          display: flex;
          align-items: baseline;
          font-size: 12px;
          font-family: PingFang SC;
          font-weight: 600;
          line-height: 17px;
          color: #fa2c1b;
          span:nth-child(2) {
            font-size: 19px;
          }
        }
        .origin-price {
          font-size: 12px;
          font-family: PingFang SC;
          font-weight: 400;
          line-height: 17px;
          color: #9b9b9b;
          text-decoration: line-through;
        }
      }
      .sales {
        font-size: 12px;
        font-family: PingFang SC;
        font-weight: 400;
        line-height: 17px;
        color: #9b9b9b;
        margin-bottom: 0;
      }
      .buy-btn {
        position: absolute;
        right: 8px;
        bottom: 4px;
        width: 24px;
        height: 24px;
        border-radius: 8px;
        display: flex;
        justify-content: center;
        align-items: center;
        .iconfont {
          font-size: 19px;
          color: #ffffff;
        }
        .icon-weibiaoti-14 {
          font-size: 20px;
          color: #333333;
        }
      }
      .buy-text {
        height: 24px;
        padding: 0 10px;
        font-size: 12px;
        font-family: PingFang SC;
        font-weight: 400;
        color: #ffffff;
        line-height: 24px;
        position: absolute;
        background-color: #fa2c1b;
        right: 8px;
        bottom: 8px;
        border-radius: 60px;
      }
      .inline-block {
        position: inherit;
        display: flex;
        margin: 4px auto;
        background-color: inherit;
        padding: 0;
        span {
          background-color: #fa2c1b;
          padding: 0 10px;
          border-radius: 60px;
        }
      }
    }
  }
  .goods-item:first-child {
    margin-left: 0 !important;
  }
  .goods-item:last-child {
    margin-right: 0 !important;
  }
}
</style>
